<template>
  <div class="app-container">
    <div class="view-title">个人信息</div>

    <el-button
      type="primary"
      @click="handlerAdd"
      v-permission="['personalInformation:add']"
      >新增个人消息</el-button
    >

    <!--      table-->

    <el-table
      :data="personalTableData"
      v-loading="loading"
      element-loading-text="正在加载中"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="text" label="内容" align="center">
      </el-table-column>
      <el-table-column prop="visibility_time" label="可见时间" align="center">
      </el-table-column>
      <el-table-column prop="user_id" label="用户id" align="center">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" align="center">
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            @click="handlerEdit(scope.row)"
            type="text"
            size="small"
            v-permission="['personalInformation:edit']"
            >修改</el-button
          >
          <el-button
            @click="handlerDelete(scope.row)"
            type="text"
            size="small"
            v-permission="['personalInformation:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--      分页-->
    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!--      新增弹窗-->
    <el-dialog title="提示" :visible.sync="dialogVisibleAdd" width="30%">
      <div class="dialog_personal_box">
        <el-form
          :model="addRuleForm"
          :rules="rules"
          ref="ruleFormAdd"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="消息内容" prop="text">
            <el-input v-model="addRuleForm.text"></el-input>
          </el-form-item>
          <el-form-item label="可见时间" prop="visibility_time">
            <el-date-picker
              v-model="addRuleForm.visibility_time"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
              @change="handlerChangeDate"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogAdd('ruleFormAdd')">取 消</el-button>
        <el-button type="primary" @click="sureDialogAdd('ruleFormAdd')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAppUserMsgList,
  addAppUserMsg,
  updateAppUserMsg,
  deleteAppUserMsg,
} from "@/api/personalInformation.js";

export default {
  name: "personalInformation",
  data() {
    return {
      personalTableData: [],
      pageNow: 1,
      pageSize: 10,
      total: null,
      dialogVisibleAdd: false,
      addRuleForm: {
        text: "",
        visibility_time: "",
      },
      rules: {
        text: [{ required: true, message: "请输入消息内容", trigger: "blur" }],
        visibility_time: [
          { required: true, message: "请选择可见时间", trigger: "blur" },
        ],
      },
      editId: "",
      deleteId: "",
      loading: true,
    };
  },
  created() {},
  mounted() {
    this.getAppUserMsgList();
  },
  methods: {
    // 获取一个用户消息
    getAppUserMsgList() {
      getAppUserMsgList({
        user_id: this.$route.query.id,
        page_now: this.pageNow,
        page_size: this.pageSize,
      }).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.personalTableData = res.data;
          this.total = res.page_info.total_count;
        } else {
          this.loading = false;
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 添加个人消息
    addAppUserMsg() {
      addAppUserMsg({
        user_id: this.$route.query.id,
        text: this.addRuleForm.text,
        visibility_time: this.addRuleForm.visibility_time,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogVisibleAdd = false;
          this.getAppUserMsgList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 修改个人消息
    updateAppUserMsg() {
      updateAppUserMsg({
        id: this.editId,
        text: this.addRuleForm.text,
        visibility_time: this.addRuleForm.visibility_time,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogVisibleAdd = false;
          this.getAppUserMsgList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 删除个人消息
    deleteAppUserMsg() {
      deleteAppUserMsg({
        ids: this.deleteId,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogFormVisible = false;
          this.getAppUserMsgList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 日期选择
    handlerChangeDate() {},

    // 新增
    handlerAdd() {
      this.dialogVisibleAdd = true;
    },

    // 新增弹窗-确定
    sureDialogAdd(ruleFormAdd) {
      this.$refs[ruleFormAdd].validate((valid) => {
        if (valid) {
          if (this.editId) {
            this.updateAppUserMsg();
          } else {
            this.addAppUserMsg();
          }
        } else {
          return false;
        }
      });
    },

    // 新增弹窗-取消
    cancelDialogAdd(ruleFormAdd) {
      this.addRuleForm = this.$options.data().addRuleForm;
      this.$refs[ruleFormAdd].clearValidate();
      this.dialogVisibleAdd = false;
    },

    // 修改
    handlerEdit(val) {
      this.editId = val.id;
      this.addRuleForm = JSON.parse(JSON.stringify(val));
      this.dialogVisibleAdd = true;
    },

    // 删除
    handlerDelete(val) {
      this.deleteId = val.id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteAppUserMsg();
        })
        .catch(() => {});
    },

    // 分页
    handleSizeChange(val) {},
    handleCurrentChange(val) {},
  },
};
</script>

<style scoped lang="scss">
.personal_information_container {
  padding: 20px 30px;

  .personal_information_box {
    .top_box {
      padding: 0 0 20px 0;
    }

    .page_box {
      display: flex;
      justify-content: center;
      padding: 20px 0;
    }
  }
}
</style>
